import util from '@/libs/util';
import getDate from '@/util/getDate.js';
import Cookies from 'js-cookie';
import memberList from './member/member.vue';
import CONSTANTS from '@/config/constants';
import diplomaList from '@/views/diploma/diploma.vue';

export default {
    props: {
        parentid: ''
    },
    components: { memberList, diplomaList },
    data () {
        return {
            columns: [{
                title: '主题名称',
                key: 'name'
            }, {
                title: '所属组织',
                key: 'organizename',
                render: (h, params) => {
                    return h('div', {}, params.row.organize.orgName);
                }
            }, {
                title: '创建时间',
                key: 'createtime',
                render: (h, params) => {
                    return h('div', {}, getDate.getDate(params.row.createtime, 'time'));
                }
            }, {
                title: '操作',
                key: 'action',
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.themeId = params.row.id;
                                    this.diplomaModal = true;
                                }
                            }
                        }, '设置奖项'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.themeId = params.row.id;
                                    this.memberModal = true;
                                }
                            }
                        }, '会员管理'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showEdit(params.row);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除')
                    ]);
                }
            }],
            baseuser: JSON.parse(Cookies.get('baseUser')),
            data: [],
            formData: {},
            viewModal: false,
            memberModal: false,
            oldIds: [],
            newIds: [],
            themeId: '',
            diplomaModal: false
        };
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/theme/list', util.toFormData({
                parentid: this.parentid
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        showEdit (obj) {
            let formData = {};
            for (var key in obj) {
                formData[key] = obj[key];
            }
            this.formData = formData;
            this.viewModal = true;
        },
        save () {
            let formData = {};
            for (let key in this.formData) {
                formData[key] = this.formData[key];
            }
            formData['createtime'] = getDate.getDate(formData.createtime, 'time');
            util.ajax.post('/busine/theme/save', util.toFormData(formData)).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.viewModal = false;
                    this.$Message.success('保存成功！');
                    this.loadList();
                } else {
                    this.$Message.warning(res.data.message || '未知错误！');
                }
            });
        },
        delete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '<p>确定删除？</p>',
                onOk: () => {
                    util.ajax.post('/busine/theme/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                        }
                    });
                },
                onCancel: () => {
                }
            });
        },
        onSelect (memberIds) {
            this.newIds = memberIds;
        },
        saveMember () {
            util.ajax.post('/busine/theme/member/save', util.toFormData({
                themeid: this.themeId,
                memberids: this.newIds
            })).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('会员设置成功！');
                } else {
                    this.$Message.warning(res.data.message || '未知错误！');
                }
                this.memberModal = false;
            });
        }
    }
};